后台服务中无摄像机视图的java opencv图像处理
我想在安卓 studio中使用opencv创建一个用于图像处理的应用程序。我没有Android Java编码的经验。这就是为什么我想得到很多建议。当前项目是从服务中的相机图像(而不是主活动)接收帧,然后处理图像。这里的问题是后台服务必须能够进行图像处理。目前的型号是Nexus5。启动应用程序时,将显示摄像头图像。此时,如果执行home(主页)按钮或其他应用程序,则应在后台连续输入图像数据,并可进行图像处理。我现在有很多搜索结果,我得到的答案是surfacetexture可以做到这一点。但是,无论您如何使用surfacetexture运行其他应用程序,都可以在后台使用图像数据进行图像处理 我不知道我是否能做到这一点。以下是我的主要活动和服务
public class MainActivity extends Activity{
private static final String TAG = MainActivity.TAG;
private Camera mCamera;
public static TextureView mTextureView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mTextureView = new TextureView(this);
setContentView(mTextureView);
setContentView(R.layout.activity_main);
startService(new Intent(this, VisionService.class));
}
}
public class VisionService extends Service implements TextureView.SurfaceTextureListener {
private static final String TAG = VisionService.class.getName();
private WindowManager windowManager;
private Camera mCamera = null;
private TextureView mTextureView;
private SurfaceTexture mSurfaceTexture;
private float[] mTransformMatrix;
public static Vibrator mVibrator;
private final IBinder mBinder = new LocalBinder();
@Override
public void onCreate() {
super.onCreate();
try {
mTextureView = MainActivity.mTextureView;
mTextureView.setSurfaceTextureListener(this);
Log.i("ABC","onCreate");
} catch (Exception e) {
Log.i("ABC","onCreate exception "+e.getMessage());
e.printStackTrace();
}
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "surfaceDestroyed method");
}
public class LocalBinder extends Binder {
VisionService getService() {
// Return this instance of this service so clients can call public methods
return VisionService.this;
}
}//end inner class that returns an instance of the service.
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}//end onBind.
@Override
public void onSurfaceTextureAvailable(SurfaceTexture mSurfaceTexture, int width, int height) {
mCamera = Camera.open(1);
if (mCamera == null) {
// Seeing this on Nexus 7 2012 -- I guess it wants a rear-facing camera, but
// there isn't one. TODO: fix
throw new RuntimeException("Default camera not available");
}
try {
mCamera.setPreviewTexture(mSurfaceTexture);
mCamera.startPreview();
mCamera.setPreviewCallback(new Camera.PreviewCallback() {
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
if (data == null) return;
Camera.Size size = mCamera.getParameters().getPreviewSize();
if (size == null) return;
//This is where I start my thread that analyzes images
Log.i("TAG","Hayoon");
}
});
} catch (IOException ioe) {
// Something bad happened
}
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
mCamera.stopPreview();
mCamera.release();
return true;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
}
共 (0) 个答案